package cn.pangza.controller;

import cn.pangza.common.mybatisplus.entity.PageRequest;
import cn.pangza.common.mybatisplus.entity.PageResult;
import cn.pangza.common.utils.ResponseData;
import cn.pangza.common.utils.ValidateUtil;
import cn.pangza.user.entity.dto.MenuDto;
import cn.pangza.user.entity.dto.MenuPageDto;
import cn.pangza.user.entity.po.Menu;
import cn.pangza.user.entity.vo.MenuChildrenVo;
import cn.pangza.user.entity.vo.MenuVo;
import cn.pangza.user.service.MenuService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/menu")
public class MenuController {

    @Autowired
    MenuService menuService;

    @GetMapping("/user_menu")
    public ResponseData<List<Menu>> getUserMenu() {
        return ResponseData.success(menuService.getUserMenuWithChildren());
    }

    @PreAuthorize("@ss.hasAnyPermission('system:menu:list') or @ss.hasAnyRole('ROLE_ADMIN')")
    @PostMapping("/page")
    public ResponseData<PageResult<MenuVo>> getMenuPage(@RequestBody PageRequest<MenuPageDto> pageRequest) {
        PageResult<MenuVo> pageResult = menuService.getMenuPage(pageRequest);
        return ResponseData.success(pageResult);
    }

    @PreAuthorize("@ss.hasAnyRole('ROLE_ADMIN')")
    @GetMapping("/menu_children")
    public ResponseData<List<MenuChildrenVo>> getMenuChildren(Integer button) {
        System.out.println("button = " + button);
        if(ObjectUtils.isEmpty(button)) button = 0;
        System.out.println("button = " + button);
        return ResponseData.success(menuService.getMenuChildren(button));
    }

    @PreAuthorize("@ss.hasAnyRole('ROLE_ADMIN') or @ss.hasAnyPermission('system:menu:create')")
    @PostMapping
    public ResponseData createMenu(@Validated @RequestBody MenuDto menu, BindingResult bindingResult) {
        ResponseData responseData = ValidateUtil.doValidate(bindingResult);
        if(ObjectUtils.isNotEmpty(responseData)) return responseData;
        menuService.createMenu(menu);
        return ResponseData.success();
    }

    @PreAuthorize("@ss.hasAnyRole('ROLE_ADMIN') or @ss.hasAnyPermission('system:menu:update')")
    @PutMapping
    public ResponseData updateMenu(@RequestBody MenuDto menu) {
        menuService.updateMenu(menu);
        return ResponseData.success();
    }

    @PreAuthorize("@ss.hasAnyRole('ROLE_ADMIN') or @ss.hasAnyPermission('system:menu:read')")
    @GetMapping("/{id}")
    public ResponseData<Menu> getMenu(@PathVariable("id") Integer id) {
        return ResponseData.success(menuService.getById(id));
    }

    @PreAuthorize("@ss.hasAnyRole('ROLE_ADMIN') or @ss.hasAnyPermission('system:menu:delete')")
    @DeleteMapping("/{id}")
    public ResponseData deleteMenu(@PathVariable("id") Integer id) {
        menuService.removeById(id);
        return ResponseData.success();
    }
}
